<?php

class SelectCourse extends MY_Controller{
    public function __construct() 
    {
        parent::__construct();

    } 
        
    function index(){

        $this->load->model('Course_Model');
        $this->load->model('Course_Permission_Model');
        $memberCourse = $this->Course_Permission_Model->getAllCourseByMemberID($this->session->userdata('memberID'));

        if(count($memberCourse) > 0):
            $courseList = array();
            foreach($memberCourse as $course):
                array_push($courseList, $course->ID);
            endforeach;
            $allCourse = $this->Course_Model->getAllCourseNotRegis($courseList);
            $this->data['allCourses'] = $allCourse;
        else:   //for new user
            $allCourse = $this->Course_Model->getAllCourses();
            $this->data['allCourses'] = $allCourse->result();
        endif;
            
        $this->data['main_content'] = 'select_course';
        $this->load->view('includes/template', $this->data);
    }
    
    function AddUpdateCourse(){
        //get value from dropdrowlist
        $courseList = '';
        for($i=1;$i<=$this->db->count_all_results('COURSE');$i++):
            $courseList = $courseList.$this->input->post('course'.$i).'|';
        endfor;
        
        $courseIDList = explode("|", $courseList);
        
        $this->db->trans_start();
         for($i=0;$i<count($courseIDList);$i++):
             if($courseIDList[$i] != '' && $courseIDList[$i] != null):
                 $this->load->model('Course_Model');
                 $course = $this->Course_Model->getVideoTimesByCourseID($courseIDList[$i]);
                 
                 $data = array(
                        'MEMBER_ID' => $this->session->userdata('memberID') ,
                        'COURSE_ID' => $course->ID,
                        'TIMES' => $course->VIDEOS,
                        'ACTIVE' => false);
                 
                 $this->db->insert('course_permission', $data);
                 
                 date_default_timezone_set('Asia/Bangkok');
                 $dataLog = array(
                        'COURSE_ID' => $course->ID,
                        'COURSE_NAME' => $course->CODE,
                        'MEMBER_ID' => $this->session->userdata('memberID'),
                        'MEMBER_NAME' => $this->session->userdata('username'),
                        'REGIS_DATE' => date("Y-m-d H:i:s")
                    );
                 $this->db->insert('regis_course_log', $dataLog);
             endif;
         endfor;
         
         //insert to regis_course_log
         
         if ($this->db->trans_status() === FALSE) {
            $this->db->trans_rollback();
        } else {
            $this->db->trans_commit();
        }
         
        $this->data['main_content'] = 'home';
        $this->load->view('includes/template', $this->data);
    }
    
    function UpdateCoursePermission(){
        //get value from dropdrowlist
        $courseList = '';
        for($i=1;$i<=$this->db->count_all_results('COURSE');$i++):
            $courseList = $courseList.$this->input->post('course'.$i).'|';
        endfor;
        
        $courseIDList = explode("|", $courseList);
        
        //end get value from dropdrowlist
        
        //update video times to course permission
        for($i=1;$i<=$this->db->count_all_results('COURSE');$i++):
            for($j=0;$j<count($courseIDList);$j++):
                if($courseIDList[$j] != '' && $courseIDList[$j] != null):
                    if($i == $courseIDList[$j]):
                        $this->load->model('Course_Model');
                        $course = $this->Course_Model->getVideoTimesByCourseID($courseIDList[$j]);
                        
                        $this->load->model('Course_Permission_Model');
                        $this->Course_Permission_Model->updatePermissionTimes($course->VIDEOS, $this->session->userdata('memberID'), $course->ID);
                    endif;
                endif;
            endfor;
        endfor;
        //end update course permisison
        
        $this->data['main_content'] = 'home';
        $this->load->view('includes/template', $this->data);
    }
    
    function UpdateSelectCourse(){
        $this->load->model('Course_Permission_Model');
        $query = $this->Course_Permission_Model->getAllCourseByMemberID($this->session->userdata('memberID'));
        
        foreach($query as $course):
            if($course->TIMES > 0):
                unset($query);
            endif;
        endforeach;
//        for($i=0;$i<count($query);$i++):
//            if($query[$i]->TIMES > 0):
//                unset($query[$i]);
//            endif;
//        endfor;

        $this->data['allCourses'] = $query;
        
        $this->data['main_content'] = 'select_course';
        $this->load->view('includes/template', $this->data);
    }
}
?>
